package com.tanglinkai.mall.controller.admin;

import com.tanglinkai.mall.controller.baseControler.ABaseController;
import com.tanglinkai.mall.contstants.Constants;
import com.tanglinkai.mall.entity.po.Supplier;
import com.tanglinkai.mall.service.SupplierService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller("supplierController")
@Slf4j
@RequestMapping("admin/supplier")
public class SupplierController extends ABaseController {

    @Autowired
    private SupplierService supplierService;

    /**
     * 显示添加供应商的表单
     * @return
     */
    @GetMapping("add")
    public ModelAndView showAddForm() {
        return getSuccessResponseVO("supplier_add");
    }

    /**
     * 添加供应商
     * @param supplier
     * @return
     */
    @PostMapping("add")
    public ModelAndView addSupplier(Supplier supplier) {
        log.info("添加供应商：{}", supplier);
        supplierService.addSupplier(supplier);
        return getSuccessResponseVO("redirect:/admin/supplier/list");
    }

    /**
     * 显示供应商列表
     * @param model
     * @return
     */
    @GetMapping("list")
    public ModelAndView listSuppliers(Model model) {
        List<Supplier> suppliers = supplierService.getAllSuppliers();
        model.addAttribute(Constants.SUPPLIERS_KEY, suppliers);
        return getSuccessResponseVO("supplier_list");
    }
    /**
     * 删除供应商
     * @param id
     * @return
     */
    @GetMapping("delete")
    public ModelAndView deleteSupplier(@RequestParam("id") Integer id) {
        log.info("删除供应商，id={}", id);
        supplierService.deleteSupplierById(id);
        return getSuccessResponseVO("redirect:/admin/supplier/list");
    }
}
